local Msg = require("core.Msg")
local TitleExcel = require("excel.Title").title
local RoleAttr = require("role.RoleAttr")
local RoleFFI = require("role.RoleFFI")
local Obj = require("core.Obj")
local PfLogic = require("tencentApi.PfLogic")
local TitleDefine = require("title.TitleDefine")
local FamilyWarTequan = require("excel.FamilyWar").WarTequan
local FamilyLogicFL = require("family.FamilyLogicFL")
local ObjHuman = require("core.ObjHuman")
local Lang = require("common.Lang")


function getTitleData(human, id)
    for i = 0, human.db.titleListLen - 1 do
        if human.db.titleList[i].id == id then
            return human.db.titleList[i]
        end
    end
end

function removeTitle(human, id)
    for i = human.db.titleListLen -1, 0, -1 do
        if human.db.titleList[i].id == id then
            human.db.titleList[i].id = human.db.titleList[human.db.titleListLen -1].id
            human.db.titleList[i].createTime = human.db.titleList[human.db.titleListLen -1].createTime
            human.db.titleListLen = human.db.titleListLen - 1
            break
        end
    end
    if human.db.titleSelectID == id then
        human.db.titleSelectID = 0
    end
end

ATTR_CACHE = ATTR_CACHE or {}
function getAllAttr(human)
    for k in pairs(ATTR_CACHE) do
        ATTR_CACHE[k] = nil
    end
    local pf = RoleFFI.ffi_string(human.db.pf)
    local conf
    local t = type(human.db) == "table"
    for i = 0, human.db.titleListLen - 1 do
          conf = TitleExcel[human.db.titleList[i].id]
          if t or conf.pf == "" or conf.pf == pf then
            for k, v in pairs(conf.attr) do
                ATTR_CACHE[k] = ATTR_CACHE[k] or 0
                ATTR_CACHE[k] = ATTR_CACHE[k] + v
            end
          end
    end
    return ATTR_CACHE
end

function sendTitleList(human)
    local pf = RoleFFI.ffi_string(human.db.pf)
    local mm = Msg.gc.GC_TITLE_QUERY
    mm.selectID = human.db.titleSelectID
    local len = 0
    for i = 1, #TitleExcel do
        if TitleExcel[i].pf == "" or pf == TitleExcel[i].pf then
            len = len + 1
            mm.list[len].id = i
            local d = getTitleData(human, i)
            mm.list[len].have = d and 1 or 0
            mm.list[len].desc = TitleExcel[i].desc
            mm.list[len].source = TitleExcel[i].source
            mm.list[len].sort = TitleExcel[i].cixu
            if TitleExcel[i].time == 0 then
                mm.list[len].time = 0
            elseif d then
                mm.list[len].time = TitleExcel[i].time*60 - (os.time()-d.createTime)
            else
                mm.list[len].time = TitleExcel[i].time*60
            end
            local cnt = 0
            for k,v  in pairs(TitleExcel[i].attr) do
                cnt = cnt + 1
                mm.list[len].attr[cnt].key = k
                mm.list[len].attr[cnt].value = v
            end
            mm.list[len].attr[0] = cnt
            mm.list[len].zhandouli = RoleAttr.calHumanPower(TitleExcel[i].attr)
            
            if len ==16 then
                break
            end
        end
    end
    mm.list[0] = len
    
    local allAttr = getAllAttr(human)
    len = 0
    for k, v in pairs(allAttr) do
        len = len + 1
        mm.attr[len].key = k
        mm.attr[len].value = v
        if len == 10 then
            break
        end
    end
    mm.attr[0] = len
    mm.zhandouli = RoleAttr.calHumanPower(allAttr)
    Msg.send(mm, human.fd)
end

function sendHumanTitle(human, self)
    local msg = Msg.gc.GC_TITLE_CHANGE
    msg.objid = human.id
    local conf = TitleExcel[human.db.titleSelectID]
    if not conf then
         msg.titleID = 0
         msg.titleName = Lang.NO_HAVE
         msg.titleSource = 0
    else
        msg.titleID = human.db.titleSelectID
         msg.titleName = conf.name
         msg.titleSource = conf.source
    end
    if not self then
        Obj.sendArea(msg, human.scene_id, human.x,human.y)
    else
        Msg.send(msg, human.fd)
    end
end

function putOn(human, id)
    local conf = TitleExcel[id]
    if not conf then
        return
    end
    if id == human.db.titleSelectID then
        return
    end
    local data = getTitleData(human, id)
    if not data then
        return
    end
    if conf.time >0 then
        if os.time() - data.createTime >= conf.time*60 then
            --已过期            
          sendTitleList(human)
          sendHumanTitle(human)
          return
        end
    end    
    human.db.titleSelectID = id
    sendTitleList(human)    
    sendHumanTitle(human)
end

function putOff(human)
    local conf = TitleExcel[human.db.titleSelectID]
    if not conf then
        return
    end
    human.db.titleSelectID = 0
    sendTitleList(human)
    sendHumanTitle(human)
end


function checkExpire()
     local del
     for _,human in pairs(ObjHuman.onlineUuid) do
          del = checkExpireHuman(human)
          if del and human.fd then
              sendHumanTitle(human)
          end
     end 
end
function checkExpireHuman(human)
    local conf
    local now = os.time()
    local del
    local delcnt = 0
    for i = human.db.titleListLen -1, 0, -1 do
         conf = TitleExcel[human.db.titleList[i].id]
         if not conf or (conf.time>0 and now - human.db.titleList[i].createTime >= conf.time*60 ) then
            if human.db.titleSelectID == human.db.titleList[i].id then
                human.db.titleSelectID = 0
                del = true
            end
            human.db.titleList[i].id = human.db.titleList[human.db.titleListLen -1].id
            human.db.titleList[i].createTime = human.db.titleList[human.db.titleListLen -1].createTime
            human.db.titleListLen = human.db.titleListLen - 1          
            delcnt = delcnt + 1  
         end
    end    
    if delcnt > 0 then
        ObjHuman.doCalc(human)
    end
    return del
end

function doCalc(human)
    local attr = getAllAttr(human)
    for k, v in pairs(attr) do
        human.attr[k]  = human.attr[k] + v
    end
end


function active(human, id, send)    
    local conf = TitleExcel[id]
    if not conf then
        return
    end
    local d = getTitleData(human, id)
    if d then
        d.createTime = os.time()
    else
        if human.db.titleListLen >=16 then
            return
        end
        human.db.titleList[human.db.titleListLen].id = id
        human.db.titleList[human.db.titleListLen].createTime = os.time()
        human.db.titleListLen = human.db.titleListLen + 1
    end
    if human.db.titleSelectID == 0 then
        human.db.titleSelectID = id
        if send then
            sendHumanTitle(human)
        end
    end
end



--跨服军团战的title
function onFamilyWarTitle(human)
    for k,v in ipairs(FamilyWarTequan) do
        removeTitle(human,v.titleID)
    end     
    local limitTime = FamilyLogicFL.getFamilyLimitTime()    
    local titleID = FamilyLogicFL.getFamilyTitle(human)
    local conf = TitleExcel[titleID]
    if conf and limitTime >0 then
        active(human, titleID)
        local data = getTitleData(human, titleID)
        if data then
            data.createTime = data.createTime - (conf.time*60 -limitTime)
        end
    end    
    ObjHuman.doCalc(human)
end

function onLogin(human)
    if human.db.isMiniClient == 14 or PfLogic.isWebsite(human) then
        local data =getTitleData(human, TitleDefine.TGP_TITLE_ID)
        if not data then
            active(human, TitleDefine.TGP_TITLE_ID)
        end
--    else
--        removeTitle(human, TitleDefine.TGP_TITLE_ID)
    end
    onFamilyWarTitle(human)
    sendHumanTitle(human, true)
end

function getTitle(human)
    local conf = TitleExcel[human.db.titleSelectID]
    if not conf then
        return 0
    end
    local d = getTitleData(human, human.db.titleSelectID)
    if not d then
        return 0
    end
    return conf.source
end

function getTitleName(human)
    local conf = TitleExcel[human.db.titleSelectID]
    if not conf then
        return Lang.NO_HAVE
    end
    local d = getTitleData(human, human.db.titleSelectID)
    if not d then
        return Lang.NO_HAVE
    end
    return conf.name
end